<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="650px"
    :close-on-click-modal="false"
    :before-close="closeDialog"
  >
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="角色">
        <el-input
          placeholder="请输入角色名称"
          maxlength="20"
          size="medium"
          v-model="form.name"
        ></el-input>
      </el-form-item>

      <el-form-item
        v-permission="['menu:search']"
        prop="roles"
        label="角色菜单"
      >
        <div class="trees">
          <el-tree
            :data="menuTree"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="menuTree"
            highlight-current
            :props="defaultProps"
          >
          </el-tree>
        </div>
      </el-form-item>

      <el-form-item prop="remark" label="备注">
        <el-input
          placeholder="请输入备注"
          maxlength="20"
          v-model="form.remark"
        ></el-input>
      </el-form-item>

      <div class="dialog-btns">
        <el-button @click="addMenu" type="primary" size="medium">{{
          this.form.id ? "编辑" : "新增"
        }}</el-button>
        <el-button @click="closeDialog" size="medium">取消</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>
  
<script>
import { getMenus } from "@/api/sys/sys";
import { addRole, getRole, updateRole } from "@/api/sys/role";
export default {
  name: "RoleEdit",
  props: {
    formData: {
      type: Object,
    },
    title: {
      type: String,
    },
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      menuTree: [],
      form: {},
      rules: {
        name: [
          {
            required: true,
            min: 2,
            max: 20,
            message: "角色名称大于2小于12个字",
            trigger: "change",
          },
        ],
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
    };
  },
  created() {
    this.form = JSON.parse(JSON.stringify(this.formData));
    if (this.form.id) {
      this.getRole(this.form.id);
    }
    this.getMenusHandle();
  },
  methods: {
    addMenu() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let checkedKeys = [
            ...this.$refs.menuTree.getCheckedKeys(),
            ...this.$refs.menuTree.getHalfCheckedKeys(),
          ];
          let postData = {
            ...this.form,
            checkedKeys,
          };
          if (!this.form.id) {
            delete postData.id;
            addRole(postData).then((res) => {
              this.closeDialog("refresh");
            });
          } else {
            updateRole(postData).then((res) => {
              this.closeDialog("refresh");
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editRoleHandle(user) {
      let editData = JSON.parse(JSON.stringify(user));
      this.getMenusHandle();
      this.getRole(editData.id);
      this.dialogVisible = true;
    },
    getRole(id) {
      getRole({ id }).then((res) => {
        let data = res.data;
        this.form.id = data.id;
        this.form.name = data.name;
        this.form.remark = data.remark;
        this.$refs.menuTree.setCheckedKeys(data.checkedMenusIds);
      });
    },
    getMenusHandle() {
      getMenus().then((res) => {
        this.menuTree = res.data;
      });
    },
    editDialog() {},
    closeDialog(type) {
      if (type === "refresh") {
        setTimeout(() => {
          this.form = {};
          this.$refs.menuTree.setCheckedKeys([]);
          this.$emit("closeDialog", type === "refresh");
        }, 100);
      } else {
        this.form = {};
        this.$refs.menuTree.setCheckedKeys([]);
        this.$emit("closeDialog", type === "refresh");
      }
    },
  },
  computed: {
    dialogTitle() {
      return (this.formData.id ? "编辑 " : "新增") + this.title;
    },
  },
};
</script>

<style scoped lang="scss">
.trees {
  height: 500px;
  overflow-y: scroll;
  border: 1px solid rgb(233, 232, 233);
}
.dialog-btns {
  text-align: center;
}
</style>